﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MoetutuTranslate.Helper
{
    class FileHelper
    {/// <summary>
     /// 选择文件并且获取路径
     /// </summary>
     /// <returns></returns>
        public static string SelectFile()
        {
            String FilePath = "";
            using (OpenFileDialog dialog = new OpenFileDialog())
            {
                dialog.Multiselect = true;//该值确定是否可以选择多个文件
                dialog.Title = "请选择文件夹";
                dialog.Filter = "所有文件(*.*)|*.*";
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    FilePath = dialog.FileName;
                }
            }
            return FilePath;
        }   /// <summary>
            /// 统一写入utf8的方法
            /// </summary>
            /// <param name="FilePath"></param>
            /// <param name="Content"></param>
        public static void AppendUTF8Text(String FilePath, String Content)
        {
            File.AppendAllText(FilePath, "\r\n" + Content, Encoding.UTF8);

        } /// <summary>
          /// 用utf8读取文件
          /// </summary>
          /// <param name="path"></param>
          /// <returns></returns>
        public static string ReadContextUtf8(string path)
        {
            using (FileStream fs = new FileStream(path, FileMode.Open))
            {
                using (StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8))
                {
                    string context = sr.ReadToEnd();
                    return context;
                }
            }

        }  /// <summary>
           /// 判断文件夹是否存在
           /// </summary>
           /// <param name="FloderPath"></param>
           /// <param name="CreatFlag"></param>
           /// <returns></returns>
        public static bool FloderExits(String FloderPath, bool CreatFlag)
        {
            if (!Directory.Exists(FloderPath))
            {

                if (!CreatFlag)
                {
                    return false;
                }
                Console.WriteLine("已创建" + FloderPath);
                Directory.CreateDirectory(FloderPath);
            }
            return true;
        }

        /// <summary>
        /// 判断文件是否存在，可传flag问是否创建
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="CreateFlag"></param>
        /// <returns></returns>
        public static bool FileExits(String FilePath, bool CreateFlag = false)
        {

            if (File.Exists(FilePath))
            {
                if (CreateFlag)
                    File.AppendAllText(FilePath, "");
                return true;
            }

            else
                return false;
        }
        /// <summary>
        /// 去除重复和空行
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static List<string> DeleteRepetText(List<string> list) {

            List<string> TempList = new List<string>();
            foreach (string line in list) {

                if (line != "") {
                    if (!TempList.Contains(line.Trim()))
                    {

                        Console.WriteLine("-++++-"+line);
                        TempList.Add(line);
                    }
                }
            }
            return TempList;
        }
        public static List<String> ReadTextAllLine(String FilePath)
        {
            List<String> TempList = new List<string>();
            if (File.Exists(FilePath))
            {
                try
                {
                    TempList = File.ReadAllLines(FilePath).ToList<string>();
                }
                catch (Exception ex)
                {

                    PrintLog.Log(ex);
                }
            }
            return TempList;
        }
    }
}
